﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace SU.Center.Infa
{
    /// <summary>
    /// 基础仓储
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseReposiotry<T> : IBaseReposiotry<T> where T : class, new()
    {
        private readonly ILogger<BaseReposiotry<T>> _logger;
        private readonly UserDbContext db;

        public BaseReposiotry(UserDbContext db, ILogger<BaseReposiotry<T>> logger)
        {
            this.db = db;
            this._logger = logger;
        }

        /// <summary>
        /// 显示所有数据
        /// </summary>
        /// <returns></returns>
        public async Task<IList<T>> GetAll()
        {
            return await db.Set<T>().ToListAsync();
        }

        /// <summary>
        /// 根据条件显示所有数据
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public async Task<IList<T>> GetsAll(Expression<Func<T, bool>> where)
        {
            return await db.Set<T>().Where(where).ToListAsync();
        }


        /// <summary>
        /// 根据一条数据
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public async Task<T?> GetAsync(Expression<Func<T, bool>> where)
        {
            return await db.Set<T>().FirstOrDefaultAsync(where);
        }

        /// <summary>
        /// 添加一条数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> InsetAsync(T entity)
        {
            await db.Set<T>().AddAsync(entity); 
            return await db.SaveChangesAsync();
        }

        /// <summary>
        /// 修改一条数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> UpdateAsync(T entity)
        {
             db.Set<T>().UpdateRange(entity);
            return await db.SaveChangesAsync();
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> DeleteEntity(T entity)
        {    
                db.Set<T>().Remove(entity);
                return await db.SaveChangesAsync();    
        }

        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public async Task<int> InsertRangeEntity(IList<T> entities)
        {
            try
            {
                await db.Set<T>().AddRangeAsync(entities);
                return await db.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError("操作失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public async Task<int> UpdateRangeEntity(IList<T> entities)
        {
            try
            {
                db.Set<T>().UpdateRange(entities);
                return await db.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError("操作失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public async Task<int> DeleteRangeEntity(IList<T> entities)
        {
            try
            {
                db.Set<T>().RemoveRange(entities);
                return await db.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError("操作失败" + ex.Message);
                throw;
            }
        }

    }
}
